/**
 * Created by shenhai on 15/9/11.
 */


var async = require('async');
var mysqlUtil = require('../../../common/utils/mysqlUtil');

exports.logListService = function(req ,callback) {

    var body = req.body;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;

    var temp = "";
    if (body.username) {
        temp = "where username like '%" + body.username + "%' ";
    }

    var sql1 = "select count(*) as count from sys_log " + temp;
    var sql2 = "select * from sys_log " + temp + " order by logID desc limit " + index +"," + size;  // logID should be modified to dateCreated

    async.series([

        function (callbackInner) {
            mysqlUtil.query(sql1, function (err, result) {

                if (err) {
                    callbackInner(err, null);
                }else {
                    var count = result[0].count;
                    var temp = {count: count, totalPage:Math.ceil(count/size)};

                    callbackInner(err, temp);
                }

            });
        },

        function (callbackInner) {
            mysqlUtil.query(sql2, callbackInner);
        }

    ], callback);

};


exports.logDeleteService = function(req ,callback) {

    var body = req.body;
    if (!(body.logID && body.logID.length)) {
        callback({domain:"没有logID"}, null);
    }
    var logID = body.logID;

    var temp = "(";
    for(var i = 0, leng = logID.length;i<leng;i++) {
        if (i == leng - 1) temp = temp + logID[i];
        else temp = temp + logID[i] +",";
    }
    temp += ")";


    var sql = "delete from sys_log where logID in " + temp;
    mysqlUtil.query(sql, callback);

};